import { RefObject, useEffect, useState } from "react";

/** 检测element尺寸 */
export default function useSize(ref: RefObject<HTMLElement>) {
  const [options, setOptions] = useState({ width: 0, height: 0 });
  useEffect(() => {
    /* if (!ref || !ref.current) return;
    const rob = new ResizeObserver((entries) => {
      const {
        contentRect: { width, height },
      } = entries[0];
      setOptions({ width, height });
    });
    rob.observe(ref.current);
    return rob.disconnect; */
    setOptions(
      ref && ref.current
        ? { width: ref.current.offsetWidth, height: ref.current.offsetHeight }
        : { width: 0, height: 0 }
    );
  }, [ref]);
  return options;
}
